package com.mlnx.bluwifi.config;

import com.mlnx.bluwifi.broadcast.BluDeviceBroadCast;
import com.mlnx.bluwifi.service.WifiBluService;
import com.mlnx.bluwifi.service.iml.WifiBluPushOldService;
import com.mlnx.bluwifi.service.iml.WifiBluPushService;
import com.mlnx.bluwifi.session.WifiBluMemmerySessionManager;
import com.mlnx.device.service.LanyaDeviceService;
import com.mlnx.device.session.SessionManagerFactory;
import com.mlnx.service.base.properties.MutilDeviceProperties;

import org.shan.mq.broker.MqttBroker;
import org.shan.mq.broker.MqttTcpBroker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * Created by amanda.shan on 2019/11/18.
 */
@Configuration
public class BluWifiConfig {

    @Autowired
    private WifiBluService wifiBluService;

    @Autowired
    private MutilDeviceProperties mutilDeviceProperties;

    @Autowired
    private LanyaDeviceService lanyaDeviceService;

    @PostConstruct
    public void startBluWifiMq() throws Exception {

        if (mutilDeviceProperties.getBluWifi().isEnable()){
            MqttBroker broker = new MqttTcpBroker();

            WifiBluMemmerySessionManager sessionManager = new WifiBluMemmerySessionManager();
            broker.start(mutilDeviceProperties.getBluWifi().getMqPort(), false,
                    sessionManager, new BluDeviceBroadCast(sessionManager, lanyaDeviceService, wifiBluService));

            SessionManagerFactory.getInstance().setBluWifiSessionManager(sessionManager);
        }
    }

    @Bean
    @ConditionalOnProperty(name = "mutil.device.bluWifi.pushOld", havingValue = "true", matchIfMissing = true)
    public WifiBluPushOldService wifiBluPushOldService() {
        return new WifiBluPushOldService();
    }

    @Bean
    @ConditionalOnProperty(name = "mutil.device.bluWifi.pushOld", havingValue = "false")
    public WifiBluPushService wifiBluPushService() {
        return new WifiBluPushService();
    }

}
